<?php
class AdminMessengerBox extends CActiveRecord{
    
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }
    
    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'messenger_box';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function insertRow($sender_id,$subject,$content,$sender_name,$user_name){
        $date = date('Y-m-d H:i:s',time());
        $last_id =0;
        $connect = Yii::app()->db;
        $sql =   "INSERT INTO messenger_box (sender_id, subject, content, sender_name, receiver_name, create_date) VALUES (:sender_id, :subject, :content, :sender_name, :receiver_name, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":sender_id", $sender_id, PDO::PARAM_STR );  
        $command->bindValue ( ":subject", $subject, PDO::PARAM_STR );
        $command->bindValue ( ":content", $content, PDO::PARAM_STR );
        $command->bindValue ( ":sender_name", $sender_name, PDO::PARAM_STR );
        $command->bindValue ( ":receiver_name", $user_name, PDO::PARAM_STR );           
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );           
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    
    public function countAllRow(){
        $sql = " SELECT count(id) as count FROM messenger_box";
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $row = $command->queryRow();
        return $row;  
    }
    
    public function getAllRowPaging($begin,$end){
        $row = array();
        $sql = " SELECT id, sender_id, subject, content, sender_name, receiver_name, create_date FROM messenger_box ORDER BY id DESC LIMIT ".$begin.",".$end;
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $row = $command->queryAll();
        return $row;         
    }
    
    public function deleteMutilRow($arr_id){
        $sql = "DELETE FROM messenger_box WHERE id IN (".$arr_id.")";
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $result = $command->execute();
        return $result;     
    }
    
    public function getRowById($id){
        $row = array();
        $sql = "SELECT * FROM messenger_box WHERE id =".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $row = $command->queryRow();
        return $row;
    }
}

   